package com.zzw.java.controller;

import cn.hutool.json.JSONObject;
import com.fazecast.jSerialComm.SerialPort;
import com.zzw.java.mqtt.MqttGateway;
import com.zzw.java.utils.ResponseResult;
import com.zzw.java.service.SerialPortService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：hcy
 * @Package：com.zzw.java.serial.spring
 * @version：1.0
 * @Date：2025/8/6 14:00
 */


@RestController
@RequestMapping("/serial")
@Slf4j
public class SerialController {

    @Autowired
    private SerialPortService serialPortService;
    public List<SerialPort> serialPortList;
    public int countsend;

    @Resource
    private MqttGateway mqttGateWay;

    @GetMapping("/testSendMqtt")
    public void send() {
        // 发送消息到指定主题
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello", "你好");
        jsonObject.put("msg", "成功");
        mqttGateWay.sendToMqtt("abc", 1, jsonObject.toString());
    }

    /**
     * 获取串口列表
     *
     * @return 返回串口列表
     */
    @GetMapping("/discover")
    public ResponseResult<List<String>> discoverSerialPort() {
        List<SerialPort> list1 = serialPortService.discoverSerialPort();
        List<String> list = new ArrayList<>();
        serialPortList = list1;
        list1.forEach(item -> {
            list.add(item.getSystemPortName());
        });
        ResponseResult<List<String>> response = new ResponseResult<>(list.size() == 0 ? 500 : 1, "", list);
        return response;
    }

    /**
     * 打开串口
     *
     * @param systemPortName 串口名
     * @return 返回开启状态结果
     */
    @GetMapping("/open")
    public ResponseResult<Boolean> openSerialPort(@RequestParam(defaultValue = "COM4") String systemPortName) {
        log.info("接收到串口：{}",systemPortName);
        return serialPortService.openSerialPort(systemPortName);
    }

    /**
     * 向串口发送数据
     *
     * @param systemPortName 串口名
     * @param data           发送内容
     * @return 返回结果
     * @throws IOException 抛出IO异常
     */
    @GetMapping("/send")
    public String sendData(@RequestParam(defaultValue = "TestData") String data, @RequestParam(defaultValue = "COM4") String systemPortName) throws IOException {
        serialPortService.sendData(systemPortName, data);
        return "Data sent>>>" + (countsend++);
    }

    /**
     * 关闭串口
     *
     * @param systemPortName 串口名
     * @return
     */
    @GetMapping("/close")
    public String closeSerialPort(@RequestParam(defaultValue = "COM4") String systemPortName) {
        if (serialPortService.closeSerialPort(systemPortName)) {
            return "Serial port closed";
        } else {
            return "Failed to find port " + systemPortName;
        }
    }

}

